﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Manager;
using Model;
using ExtAspNet;
using Manager.CustomerHelper;
namespace HealthSystem.sysAdmin
{
    public partial class SchoolMana : BasePage
    {
        HW_SchoolManager manager = new HW_SchoolManager();
        public override ModuleType Module
        {
            get
            {
                return ModuleType.SchoolManager;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Newbt.OnClientClick = Window1.GetSaveStateReference(SchoolCodeBtn.ClientID)+Window1.GetShowReference("~/School_New.aspx","新增学校");
                name.OnClientTriggerClick = Window2.GetSaveStateReference(districtid.ClientID,name.ClientID)
                   + Window2.GetShowReference("~/ChooseDistrict.aspx","地区选择");
                this.actionbt.Text = "";
                BindGrid();
            }
        }
        public void BindGrid()
        {
            int count = 0;
            DataTable list = new DataTable(); ;
            Gird1.IsDatabasePaging = true;
            if (actionbt.Text == "search")
            {
                Gird1.IsDatabasePaging = false;
                Search();
            }
            else
            {
                if (!string.IsNullOrEmpty(districtid.Text))
                {
                    list = manager.GetSchoolDetail(Gird1.PageIndex, Gird1.PageSize, out count, districtid.Text);
                    //BindGrid(districtid.Text.Trim());
                }
                else
                {
                    list = manager.GetSchoolDetail(Gird1.PageIndex, Gird1.PageSize, out count, CustomerProfile.DistrictId);
                    //BindGrid(CustomerProfile.DistrictId);
                }
                //for (int i = 0; i < list.Rows.Count; i++)
                //{
                //    list.Rows[i]["permit"] = list.Rows[i]["permit"] is DBNull ? false : list.Rows[i]["permit"];
                //}
                Gird1.RecordCount = count;
                this.Gird1.DataSource = list;
                this.Gird1.DataBind();
            }
           
        }
        public void Grid1_DoubleClick(object sender, ExtAspNet.GridRowClickEventArgs e)
        {
            string schoolcode = Gird1.Rows[e.RowIndex].DataKeys[0] as string;
            string schoolname = Gird1.Rows[e.RowIndex].DataKeys[1] as string;
            string url = "~/schoolsignadmin.aspx?schoolcode=" + schoolcode;
            PageContext.RegisterStartupScript(Window1.GetShowReference(url, schoolname + "用户"));
        }
        public void Window1_Close(object sender, EventArgs e)
        {
            if (actionbt.Text == "new")
            {
                int count = 0;
                Gird1.DataSource = manager.GetSchoolBySchoolCode(0, Gird1.PageSize, out count, SchoolCodeBtn.Text);
                Gird1.RecordCount = count;
                Gird1.DataBind();
            }
            else
            {
                Gird1.PageIndex = 0;
                BindGrid();
            }
        }
        protected void PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            int pageindex = e.NewPageIndex;
            Gird1.PageIndex = pageindex;
            BindGrid();
        }
        protected void Search_Clik(object sender, EventArgs e)
        {
            //if (string.IsNullOrEmpty(searchbox.Text))
            //{
            //    ExtAspNet.Alert.Show("请输入查询条件");
            //    return;
            //}
            //DataTable dt = manager.Search(CustomerProfile.DistrictId, searchbox.Text);
            //Gird1.IsDatabasePaging = false;
            //this.Gird1.DataSource = dt;
            //this.Gird1.DataBind();
            //actionbt.Text = "search";
            Gird1.PageIndex = 0;
            Search();
        }
        private void Search()
        {

            if (string.IsNullOrEmpty(searchbox.Text))
            {
                BindGrid();
                actionbt.Text = "";
            }
            int count = 0;
            DataTable list = manager.Search(Gird1.PageIndex,Gird1.PageSize,out count,CustomerProfile.DistrictId, searchbox.Text);
            for (int i = 0; i < list.Rows.Count; i++)
            {
                list.Rows[i]["permit"] = list.Rows[i]["permit"] is DBNull ? false : list.Rows[i]["permit"];
            }
            Gird1.IsDatabasePaging = false;
            this.Gird1.DataSource = list;
            this.Gird1.DataBind();
            actionbt.Text = "search";
        }
        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            try
            {
                string schoolcode = Gird1.Rows[e.RowIndex].DataKeys[0].ToString();
                if (e.CommandName == "delete")
                {
                    manager.DeleteById(schoolcode);
                    BindGrid();

                }
                if (e.CommandName == "permit")
                {
                    try
                    {
                        string pid = this.Gird1.Rows[e.RowIndex].DataKeys[0].ToString();
                        HW_School school = manager.GetById(pid);
                        bool exist = DistrictCache.CheckExist(CustomerProfile.DistrictId, school.DistrictCode);
                        if (exist)
                        {
                            if (school.Permit.HasValue)
                            {
                                if (school.Permit.Value)
                                {
                                    school.Permit = false;
                                }
                                else
                                {
                                    school.Permit = true;
                                }
                            }
                            else
                            {
                                school.Permit = true;
                            }
                            //manager.Permit(school);
                            manager.ChangePermit(school);
                            BindGrid();
                        }
                        else
                        {
                            Alert.Show("您没有权限给该用户授权");
                        }
                    }
                    catch
                    {
                        Alert.Show("授权出错！");
                    }
                    //string pid = this.Gird1.Rows[e.RowIndex].DataKeys[1].ToString();
                    //PageContext.RegisterStartupScript(Window2.GetShowReference("Permit.aspx?id=" + pid, "学校授权"));
                }
                if (e.CommandName == "reset")
                {
                    //重置密码
                    //HW_School_UserManager orgUserManager = new HW_School_UserManager();
                    //HW_OrgUser orgUser = orgUserManager.GetById(username);
                    //orgUser.XX_Pass = System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    //orgUserManager.ModHW_OrgUser(orgUser);
                    //Alert.Show("密码重置成功");
                    //BindGrid();
                }
            }
            catch
            {
                Alert.Show("操作失败!");
            }
        }
    }
}